var express = require('express');
var router = express.Router();

//引入服务层
const cinemaService = require('../src/services/cinemasService.js');

//引入 upload 文件
const { uploadFile } = require('../src/utils/upload.js');

/* GET home page. */
//查询
router.get('/getall', async function (req, res) {
  let { currentpage, pagesize } = req.query;
  //分页
  let pageBean = await cinemaService.findAll(
    {
      "currentPage": parseInt(currentpage),
      "pageSize": parseInt(pagesize)
    });
  res.json({ 'code': 0, 'msg': '查询成功', ...pageBean });
});

//根据 id 查询
router.get('/getone/:id', async function (req, res) {
  let { id } = req.params;
  console.log(id);
  let result = await cinemaService.findone(id);
  console.log(result);
  if (result) {
    res.json({ 'code': 1, 'msg': '查询成功', 'count': 1, 'data': result });
  } else {
    res.json({ 'code': 0, 'msg': '查询失败' });
  }
});

//根据地区查询
router.get('/getaddress/:address', async function (req, res) {
  let { address } = req.params;
  let result = await cinemaService.addressQuery(address);
  if (result) {
    res.json({ 'code': 0, 'msg': '查询成功', 'count': 1, 'data': result });
  } else {
    res.json({ 'code': 0, 'msg': '查询失败' });
  }
});

//新增
router.post('/addcinema', async function (req, res) {
  //上传文件
  let datas = await uploadFile(req, { fileType: "images" });
  console.log(datas);
  //1 获得上传文件的名字  '/photos/fa22def2fe792.jpg'
  let photo = datas.data.url.split('/')[2];
  // //添加影院
  // let obj = {}; 
  // //解构参数的值
  let { name, address, phone, status } = datas.data;
  // //构建影院对象
  // obj = { name, address, phone, status, photo };
  // let { name, address, phone, status } = req.body;
  let result = await cinemaService.addCinema(name, address, phone, status, photo);
  console.log(result);
  if (result) {
    res.json({ "code": 1, "msg": "添加成功", 'datas': result });
  } else {
    res.json({ "code": 0, "msg": "添加失败" });
  }
});

//删除
router.delete('/delcinema/:id', async function (req, res) {
  let { id } = req.params;
  let result = await cinemaService.delCinema(id);
  if (result.deletedCount == 1) {
    res.json({ 'code': 1, 'msg': '删除成功', 'datas': result });
  } else {
    res.json({ 'code': 0, 'msg': '删除失败' });
  }
});

//图片修改
router.post('/editcinema', async function (req, res) {
  let datas = await uploadFile(req, { fileType: "images" });
  let photo = datas.data.url.split('/')[2];
  let {_id, name, address, phone, status } = datas.data;

  // let { _id, name, address, phone, status, photo } = req.body;
  let result = await cinemaService.editCinema(_id, name, address, phone, status, photo);
  if (result.modifiedCount) {
    res.json({ "code": 1, "msg": "修改成功", "data": result });
  } else {
    res.json({ "code": 0, "msg": "修改失败" });
  }
});

//修改
router.put('/editext', async function (req, res) {
  let { _id, name, address, phone, status } = req.body;
  let result = await cinemaService.edited(_id, name, address, phone, status);
  if (result.modifiedCount) {
    res.json({ "code": 1, "msg": "修改成功", "data": result });
  } else {
    res.json({ "code": 0, "msg": "修改失败" });
  }
});


module.exports = router;
